package com.example.doctorsystem.controller;

import com.example.doctorsystem.model.Prescription;
import com.example.doctorsystem.service.PrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/prescriptions")
public class PrescriptionController {

    @Autowired
    private PrescriptionService prescriptionService;

    @PostMapping("/create")
    public String createPrescription(@RequestBody Prescription prescription) {
        try {
            int result = prescriptionService.createPrescription(prescription);
            return result > 0 ? "处方创建成功" : "创建失败";
        } catch (IllegalArgumentException e) {
            return e.getMessage();
        }
    }

    @GetMapping("/doctor/{doctorId}")
    public List<Prescription> getByDoctor(@PathVariable int doctorId) {
        return prescriptionService.getPrescriptionsByDoctorId(doctorId);
    }

    @GetMapping("/patient/{patientId}")
    public List<Prescription> getByPatient(@PathVariable int patientId) {
        return prescriptionService.getPrescriptionsByPatientId(patientId);
    }

    @GetMapping
    public List<Prescription> getAll() {
        return prescriptionService.getAllPrescriptions();
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable int id) {
        try {
            boolean success = prescriptionService.deletePrescription(id);
            return success ? "删除成功" : "删除失败";
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}
